package com.zhiqianbao.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zhiqianbao.R;
import com.zhiqianbao.entity.Repay;
import com.zhiqianbao.entity.Result;
import com.zhiqianbao.entity.UserBank;
import com.zhiqianbao.entity.UserInfo;
import com.zhiqianbao.utils.Constants;
import com.zhiqianbao.utils.HttpUtils;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import cn.trinea.android.common.util.StringUtils;

/**
 * Created by liang on 2016/1/14.
 */
public class SelectBankActivity extends BaseActivity implements View.OnClickListener {
    /**
     * 支付内容
     */
    TextView textViewLineOfPayContent;
    /**
     * 支付金额显示
     */
    TextView textViewPayMoneyContent;
    /**
     * 银行卡信息显示
     */
    TextView textViewPayBankContent;
    /**
     * 银行卡信息
     */
    TextView textViewPayBankTitle;
    /**
     *
     */
    RelativeLayout relativeLayoutBank;
    /**
     * 使用其他银行卡按钮
     */
    Button buttonConfirm;

    //还款期数数据
    Repay repay;
    //银行卡信息
    UserBank userBank;
    UserInfo userInfo;
    String bankcard;

    Handler checkQuickPayHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case RESULT_CANCELED :
                    showShortToast1(Constants.HTTP_ERROR_MESSAGE);
                    break;
                case RESULT_OK :
                    Result<String> data = (Result<String>) msg.obj;
                    if(data!=null) {
                        if (data.getCode().equals(Constants.DEFAULT_SUCCESS_CODE)) {
                            String quickPayState = data.getData();
                            //未开通快捷支付,首先开通快捷支付
                            if ("0".equals(quickPayState)) {
                                //跳转到开通快捷支付的页面
                                Intent openQuickPayIntent = new Intent(SelectBankActivity.this, BindBankConfirmActivity.class);
                                Map<String, Object> dataMap = new HashMap<>();
                                dataMap.put("bankCard", userBank.getBankCard());
                                dataMap.put("bankName", userBank.getDeposit());
                                dataMap.put("name", userInfo.getName());
                                dataMap.put("idcard", userInfo.getIdcard());
                                dataMap.put("repay", repay);
                                String dataMapJson = gson.toJson(dataMap);
                                openQuickPayIntent.putExtra("dataJson", dataMapJson);
                                startActivity(openQuickPayIntent);
                            } else {
                                Intent confrimBankIntent = new Intent(SelectBankActivity.this, RepayBankConfirmActivity.class);
                                confrimBankIntent.putExtra("repay", repay);
                                confrimBankIntent.putExtra("userBank", userBank);
                                startActivity(confrimBankIntent);
                            }
                        } else {
                            showShortToast1(data.getMsg());
                        }
                    }
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_bank);
        init();

        Intent intent = getIntent();
        repay = (Repay) intent.getSerializableExtra("repay");
        String id = Constants.LOGIN_USER.getId().toString();
        List<UserBank> userBanks = UserBank.findWithQuery(UserBank.class, "select * from user_bank where user_id = ? ", id);
        userBank = userBanks.get(0);
        List<UserInfo> userInfos = UserInfo.findWithQuery(UserInfo.class, "select * from user_info where user_id = ? ", id);
        userInfo = userInfos.get(0);

        initView();

    }

    private void initView() {
         textViewLineOfPayContent=(TextView)findViewById(R.id.textView_line_of_pay_content);
         textViewPayMoneyContent=(TextView)findViewById(R.id.textView_pay_money_content);
         textViewPayBankContent=(TextView)findViewById(R.id.textView_pay_bank_content);
         textViewPayBankTitle=(TextView)findViewById(R.id.textView_pay_bank_title);
         relativeLayoutBank=(RelativeLayout)findViewById(R.id.relativeLayout_bank);
         buttonConfirm=(Button)findViewById(R.id.button_confirm);
        //期数
        int term = repay.getTerms();
        String termStr = "偿还借款第" + term + "期";
        //金额
        DecimalFormat df = new DecimalFormat("#.00");
        String moneyStr = df.format(repay.getRepayTotal());

        textViewLineOfPayContent.setText(termStr);
        textViewPayMoneyContent.setText(moneyStr);

        bankcard = userBank.getBankCard();
        int l = bankcard.length() -  8;
        String x = "";
        for (int i = 0; i < l; i++) {
            x += "*";
        }
        bankcard = bankcard.substring(0, 4) + x + bankcard.substring(bankcard.length() - 4, bankcard.length());
        textViewPayBankContent.setText(bankcard);
        textViewPayBankTitle.setText(userBank.getDeposit());

        relativeLayoutBank.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if(R.id.relativeLayout_bank==v.getId()) {
                //查询此卡是否开通快捷支付
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Map<String, String> params = new HashMap<>();
                        params.put("token", Constants.LOGIN_USER != null ? Constants.LOGIN_USER.getToken() : "");
                        params.put("userId", Constants.LOGIN_USER != null ? Constants.LOGIN_USER.getId() + "" : "");
                        params.put("bankCard", userBank.getBankCard());
                        String result = HttpUtils.httpPostString(Constants.API_ROOT + "/user/checkUserBankCardQuickPay", params, SelectBankActivity.this);
                        Message message = new Message();
                        if (StringUtils.isBlank(result)) {
                            message.what = RESULT_CANCELED;
                        } else {
                            message.what = RESULT_OK;
                            Result<String> data = Result.fromJson(result, String.class);
                            message.obj = data;
                        }
                        checkQuickPayHandler.sendMessage(message);
                    }
                }).start();

        }

    }
}
